﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using System.Web.Mvc;
using SPlusres.Business;

namespace SPlusres.Controller
{
    public class LoginController : BaseController
    {
        /*////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        * System Property Area
        */
        /// <summary>
        /// unLogin User Flag
        /// </summary>
        const int UNLOGINVALUE = -1;

        /// <summary>
        /// user id
        /// </summary>
        private int _userid = UNLOGINVALUE;

        /// <summary>
        /// User Flag
        /// </summary>
        public int userid
        {
            get
            {
                if (_userid == UNLOGINVALUE)
                {
                    string userid = string.Empty;

                    if (System.Web.HttpContext.Current.Request.Cookies["userid"] != null && (userid = DES.Decode(System.Web.HttpContext.Current.Request.Cookies["userid"].Value, SystemSetting.EncryptKey)).IsNumber())
                        _userid = int.Parse(userid);
                }

                return (_userid);
            }
        }

        /// <summary>
        /// User Name
        /// </summary>
        public string UserName
        {
            get
            {
                return System.Web.HttpContext.Current.Request.Cookies["UserName"].Value;
            }
        }
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// check user in login
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
        {
            if (!filterContext.ActionDescriptor.IsDefined(typeof(Attributes.WithOutLogin), false))
                if (userid == UNLOGINVALUE)
                    filterContext.Result = View("unlogin");
        }
    }
}
